<?php
declare (strict_types=1);

namespace app\school\service\school;

use app\common\service\BaseService;
use app\school\logic\school\School as SchoolLogic;
use think\facade\Event;
use think\facade\Request;

/**
 * 机构管理
 */
class School extends BaseService
{
    /**
     * 机构列表
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-05-29
     */
    public function index()
    {
        // 搜索功能
        $map[] = ['id', '<>', 1];
        // Request::has('title', 'get', true) && $map['title'] = Request::get('title/s');
        // input('?get.title') && $map['title'] = input('get.title/s');
        if (input('?get.category') && input('get.category/d')) {
            $map[] = ['category', 'LIKE', '%,' . input('get.category/d') . ',%'];
        }
        $data = app(SchoolLogic::class)->getList($map);

        return $this->parseToData($data, 1, '获取成功');
    }

    public function search()
    {
        $title = Request::has('title', 'get', true) ? Request::get('title/s') : '';
        $data  = app(SchoolLogic::class)->search($title);

        return $this->parseToData($data, 1, '获取成功');
    }

    // 机构主页
    public function home(int $id)
    {
        $data = app(SchoolLogic::class)->getHomePage($id);

        Event::trigger('WebsiteVisit', [
            'mhm_id'        => $data['info']->id,
            'resource_type' => 'homepage',
            'resource_id'   => $id,
        ]);

        return $this->parseToData($data, 1, '获取成功');
    }

    // 机构信息
    public function info(int $id)
    {
        $data = app(SchoolLogic::class)->getInfo($id);
        return $this->parseToData($data, 1, '获取成功');
    }

    // 机构课程
    public function course(int $id)
    {
        $map[] = ['mhm_id', '=', $id];//request()->mhm_id
        $map[] = ['status', '=', 1];
        $map[] = ['listing_time', '<', time()];

        if (input('?get.category') && input('get.category/d')) {
            $map[] = ['mhm_category', 'LIKE', '%,' . input('get.category/d') . ',%'];
        }
        input('?get.course_type') && $map[] = ['course_type', '=', input('get.course_type/d')];

        $data = app(SchoolLogic::class)->getCourse($map);
        return $this->parseToData($data, 1, '获取成功');
    }

    // 机构认证页面
    public function authFetch()
    {
        $data = app(SchoolLogic::class)->authFetch();
        return $this->parseToData($data, 1, '获取成功');
    }

    // 机构提交认证
    public function authSubmit()
    {
        $param = Request::post();
        $data  = app(SchoolLogic::class)->authSubmit($param);
        return $this->parseToData($data, 1, '提交成功，请等待审核');
    }

}
